/********************************************************/
/*	Copyright (C) 2016 Gong Li Bin		 	*/
/*	Project:	GlbRtbEng-1.0.0			*/
/*	Author:		gong_libin			*/
/*	Date:		2016_06_26			*/
/*	File:		GlbRtbRestTool.cpp		*/
/********************************************************/

#include "GlbTcp.h"
#include "GlbFile.h"
#include "GlbUtil.h"
#include <getopt.h>

static char* sg_pszFile;
static char* sg_pszPort;

void GlbRtbRestCore(int iSocket);
int GlbRtbRestHttp(char* pszRecv, char* pszSend);

int main(int argc, char* argv[])
{
 	int iIndex = 0;
	int iReturn = 0;
	GlbNet::CGlbTcp CTcp;
	struct option stOpt[] = {
		{"file", 1, 0, 'f'},
		{"port", 1, 0, 'p'},
		{"version", 0, 0, 'v'},
		{"help", 0, 0, 'h'},
		{0, 0, 0, 0},
	};

	sg_pszPort = (char*)"80";
	sg_pszFile = (char*)"/root/GlbRtbConf-1.0.0/GlbRtbAdvs.xml";

	while (GLB_FAILURE != (iReturn = getopt_long(argc, argv, "f:p:vh", stOpt, &iIndex))) {
		switch (iReturn) {
			case 'f':
				sg_pszFile = optarg;
				break;
			case 'p':
				sg_pszPort = optarg;
				break;
			case 'v':
				GLB_PRINT("Copyright (C) 2016 Gong Li Bin	\n");
				GLB_PRINT("GlbRtbRestTool Build on %s %s.\n", __DATE__, __TIME__);
				return GLB_SUCCESS;
			case 'h':
				GLB_PRINT("Usage: %s [options]\n", argv[0]);
				GLB_PRINT("Valid options are:\n");
				GLB_PRINT("[0] -f or --file:       set file path\n");
				GLB_PRINT("[0] -p or --port:       set listen port\n");
				GLB_PRINT("Copyright (C) 2016 Gong Li Bin	\n");
				GLB_PRINT("GlbRtbRestTool Build on %s %s.\n", __DATE__, __TIME__);
				return GLB_SUCCESS;
			default:
				return GLB_SUCCESS;
		}
	}

	if (GLB_SUCCESS == CTcp.GlbTcpInit(sg_pszPort)) {
		GLB_PRINT("Listenning %s ...\n", sg_pszPort);
		CTcp.GlbTcpSetFunction(GlbRtbRestCore);
		CTcp.GlbTcpAccept();
	}
	else {
		GLB_ERROR("Failed to GlbTcpInit\n");
	}

	return GLB_SUCCESS;
}

void GlbRtbRestCore(int iSocket)
{
	GlbNet::CGlbTcp CTcp;
	char szRecv[GLB_KBYTES8] = {0};
	char szSend[GLB_KBYTES8] = {0};

	CTcp.GlbNetSetSocket(iSocket);

	if (CTcp.GlbNetRecv((UCHAR*)szRecv, GLB_KBYTES8) > 0) {
		if (GLB_SUCCESS == GlbRtbRestHttp(szRecv, szSend)) {
			CTcp.GlbNetSend((UCHAR*)szSend, strlen(szSend));
		}
	}
	else {
		GLB_PRINT("Failure to GlbNetRecv\n");
	}

	CTcp.GlbNetClose();

	return;
}

int GlbRtbRestHttp(char* pszRecv, char* pszSend)
{
	ULONG ulSize = 0;
	GlbCls::CGlbFile CFile;
	char* pszBody = NULL;
	int iReturn = GLB_SUCCESS;
	char szTime[GLB_DAYTIME] = {0};
	char szHead[GLB_BYTE512] = 
		"HTTP/1.1 200 OK\r\n"\
		"Date: %s\r\n"\
		"Content-Type: text/html;charset=ISO-8859-1\r\n"\
		"Content-Length: %d\r\n"\
		"\r\n";

	if (GLB_SUCCESS == CFile.GlbFileOpen(sg_pszFile, GLB_O_RDONLY)) {
		CFile.GlbFileMmap(GLB_P_READ, GLB_M_PRIVATE, 0);

		GlbLocalDayTime(szTime);
		ulSize = CFile.GlbFileGetSize();
		pszBody = (char*)CFile.GlbFileGetFile();

		sprintf(pszSend, szHead, szTime, strlen(pszBody));
		memcpy(pszSend + strlen(pszSend), pszBody, ulSize);

		GLB_PRINT("\nRecv:\n%s\n", pszRecv);
		GLB_PRINT("\nSend:\n%s\n", pszSend);

		CFile.GlbFileMunmap(0);
		CFile.GlbFileClose();
	}
	else {
		iReturn = GLB_FAILURE;
	}

	return iReturn;
}
